package com.example.yskjproject.ui.activity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import com.example.yskjproject.R;
import com.example.yskjproject.base.BaseActivity;
import com.example.yskjproject.content.EventContent;
import com.example.yskjproject.databinding.ActivityNewPhoneAlertBinding;
import com.example.yskjproject.event.EventMessage;
import com.example.yskjproject.http.Urls;
import com.example.yskjproject.http.callback.JsonCallback;
import com.example.yskjproject.model.LzyResponse;
import com.example.yskjproject.model.PhoneNumberVerifyModel;
import com.example.yskjproject.utils.EventBusUtil;
import com.example.yskjproject.utils.IntentUtils;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.tencent.qcloud.tuicore.util.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class NewPhoneBindActivity extends BaseActivity<ActivityNewPhoneAlertBinding> {
    ActivityNewPhoneAlertBinding mActivityNewPhoneAlertBinding;
    private String phoneRegex = "^1[3-9]\\d{9}$";
    private CountDownTimer countDownTimer;
    private String rechangeCN;
    private Gson gson = new Gson();
    @Override
    protected ActivityNewPhoneAlertBinding getViewBinding() {
        mActivityNewPhoneAlertBinding = ActivityNewPhoneAlertBinding.inflate(LayoutInflater.from(this));
        Intent intent = new Intent();
        rechangeCN = intent.getStringExtra("data");
        return mActivityNewPhoneAlertBinding;
    }

    @Override
    protected void initData() {
        setTitle("新手机号绑定");
        mActivityNewPhoneAlertBinding.getVerification.setOnClickListener(v -> {
            if (TextUtils.isEmpty(mActivityNewPhoneAlertBinding.phoneEditText.getText())){
                ToastUtil.toastLongMessage("手机号不能为空");
                return;
            }
            if (!mActivityNewPhoneAlertBinding.phoneEditText.getText().toString().matches(phoneRegex)) {
                ToastUtil.toastLongMessage("请输入正确的手机号码");
            } else {
                getVerification();
            }

        });

        mActivityNewPhoneAlertBinding.btnTrue.setOnClickListener(v -> {
            if (TextUtils.isEmpty(mActivityNewPhoneAlertBinding.phoneEditText.getText())){
                ToastUtil.toastLongMessage("手机号不能为空");
            } else if (TextUtils.isEmpty(mActivityNewPhoneAlertBinding.verification.getText())){
                ToastUtil.toastLongMessage("验证码不能为空");
            } else {
                AlertNewPhone();
            }
        });

    }

    private void AlertNewPhone() {
        Map<String, Object> jsonParams = new HashMap<>();
        jsonParams.put("mobile", mActivityNewPhoneAlertBinding.phoneEditText.getText());
        jsonParams.put("code",mActivityNewPhoneAlertBinding.verification.getText());

        String paramsDate = gson.toJson(jsonParams);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), paramsDate);
        OkGo.<LzyResponse<PhoneNumberVerifyModel>>post(Urls.VERIFY_NEW_PHONE).upRequestBody(requestBody).execute(new JsonCallback<LzyResponse<PhoneNumberVerifyModel>>() {
            @Override
            public void onSuccess(Response<LzyResponse<PhoneNumberVerifyModel>> response) {
                ToastUtil.toastLongMessage("绑定手机号成功");
                EventMessage eventMessage = new EventMessage(EventContent.upDate_phone, mActivityNewPhoneAlertBinding.phoneEditText.getText().toString());
                EventBusUtil.sendEvent(eventMessage);
                finish();

            }

            @Override
            public void onError(Response<LzyResponse<PhoneNumberVerifyModel>> response) {
                super.onError(response);
                ToastUtil.toastShortMessage("绑定手机号失败");
            }
        });

    }

    private void getVerification() {
        OkGo.<LzyResponse>get(Urls.YZM_CODE + "?mobile=" + mActivityNewPhoneAlertBinding.phoneEditText.getText().toString().trim() + "&type=" + 2).execute(new JsonCallback<LzyResponse>() {
            @Override
            public void onSuccess(Response<LzyResponse> response) {
                if (TextUtils.equals("ok", response.body().msg)) {
                    ToastUtil.toastShortMessage("获取手机验证码成功!");
                    countDownTimer = new CountDownTimer(60000, 1000) {
                        public void onTick(long millisUntilFinished) {
                            mActivityNewPhoneAlertBinding.getVerification.setText(millisUntilFinished / 1000 + "s");
                        }

                        public void onFinish() {
                            mActivityNewPhoneAlertBinding.getVerification.setText("获取验证码");
                        }
                    }.start();

                } else {
                    ToastUtil.toastShortMessage("获取手机验证码失败!");
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel();
            countDownTimer = null;
        }
    }
}
